﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebUserControl_ControlKhaiBaoTTPP : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ViewState["flag"] == null)
            {
                ViewState["flag"] = false;
            }
        }
    }
    protected void btnNew_Click(object sender, ImageClickEventArgs e)
    {
        MakeNew();
    }
    void MakeNew()
    {
        txtMaTTPP.ReadOnly = false;
        txtMaTTPP.Text = "";
        txtDiaChi.Text = "";
        txtTenTTPP.Text = "";
        txtDienThoai.Text = "";
        txtSAP.Text = "";
        CheckShowRoom.Checked = false;
       ViewState["flag"] = true;
    }
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        bool kq = false;
        if ((bool)ViewState["flag"])
        {
            string i = dt.GetDataDepend(str, "ls_isDuplicateSiteCode", CommandType.StoredProcedure, ref error,
                new SqlParameter("@siteCode", txtMaTTPP.Text));
            if (i != "1")
            {
                kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "TableMisInsert", ref error,
                    new SqlParameter("@Sitecode", txtMaTTPP.Text),
                    new SqlParameter("@sitename", txtTenTTPP.Text),
                    new SqlParameter("@Address", txtDiaChi.Text),
                    new SqlParameter("@Tel", txtDienThoai.Text),
                    new SqlParameter("@Active", CheckActive.Checked),
                    new SqlParameter("@isShowRoom",CheckShowRoom.Checked),
                    new SqlParameter("@SAPCode",txtSAP.Text));
                if (kq)
                {
                    uscMsgBox1.AddMessage("Khai báo TTPP mới thành công", uscMsgBox.enmMessageType.Success);
                    ViewState["flag"] = false;
                }
                else
                {
                    uscMsgBox1.AddMessage("Mã TTPP bị trùng,nhập lại", uscMsgBox.enmMessageType.Error);

                }
            }
            else
            {
                uscMsgBox1.AddMessage("Mã TTPP bị trùng,nhập lại", uscMsgBox.enmMessageType.Error);
            }
        }
        else
        {
            kq=dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "TableMisUpdate", ref error,
               new SqlParameter("@Sitecode", txtMaTTPP.Text),
               new SqlParameter("@sitename", txtTenTTPP.Text),
               new SqlParameter("@Address", txtDiaChi.Text),
               new SqlParameter("@Tel", txtDienThoai.Text),
               new SqlParameter("@Active", CheckActive.Checked));
            if (kq)
            {
                uscMsgBox1.AddMessage("Cập nhật TTPP thành công", uscMsgBox.enmMessageType.Success);
            }
            else
            {
                uscMsgBox1.AddMessage("Cập nhật TTPP thất bại", uscMsgBox.enmMessageType.Error);

            }
        }
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
        FindTTPP();

    }
    void FindTTPP()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getTTPP", CommandType.StoredProcedure,
            new SqlParameter("@para", txtSearch.Text));
        if (db.Rows.Count > 0)
        {
            GridView2.DataSource = db;
            GridView2.DataBind();
        }
        else
        {
            uscMsgBox1.AddMessage("Không tìm thấy dữ liệu tương ứng", uscMsgBox.enmMessageType.Success);
        }
       
    }
    protected void ImgTimKiem_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "chon")
        {
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            dt = new ClassExecute.ClassExecute();
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getFullTTPP", CommandType.StoredProcedure,
                new SqlParameter("@sitecode", grv.Cells[0].Text));
            if (db.Rows.Count > 0)
            {
                txtMaTTPP.Text = db.Rows[0]["Sitecode"].ToString();
                txtTenTTPP.Text = db.Rows[0]["sitename"].ToString();
                txtDiaChi.Text = db.Rows[0]["Address"].ToString();
                txtDienThoai.Text = db.Rows[0]["Tel"].ToString();
                CheckActive.Checked = bool.Parse(db.Rows[0]["Active"].ToString());
                CheckShowRoom.Checked = bool.Parse(db.Rows[0]["isShowRoom"].ToString());
                txtSAP.Text = db.Rows[0]["company_code"].ToString();
                TabContainer1.ActiveTabIndex = 0;
                ViewState["flag"] = false;
                txtMaTTPP.ReadOnly = true;

            }
        }
    }
    protected void btFind_Click(object sender, EventArgs e)
    {
        FindTTPP();
    }
}
